<?php
// MY_FrontController in application/core/MY_Controller.php
class Index extends MY_FrontController {
	public function __construct()
	{
		parent::__construct();
	}
	
	public function index()
	{		
		$seg = $this->uri->segment(1);
		$link = array();
		$link["intro"] = stripUnicode($this->data["lang"]["navi"]["intro"]);
		$link["product"] = stripUnicode($this->data["lang"]["navi"]["product"]);
		$link["news"] = stripUnicode($this->data["lang"]["navi"]["news"]);
		$link["contact"] = stripUnicode($this->data["lang"]["navi"]["contact"]);
		$this->data["curr_link"] = base_index;
		if(in_array($seg, $this->data["header_link"]["intro"])){
			$this->data["curr_link"] .= $link["intro"] . "/";
			$this->data["menu_active"] = "intro";
			$this->get_link();
			$this->intro();
		}
		elseif(in_array($seg, $this->data["header_link"]["product"])){
			$this->data["menu_active"] = "product";
			$this->get_link();
			$this->data["curr_link"] .= $link["product"] . "/";
			$this->data["curr_menu"] = $this->data["lang"]["navi"]["product"];
			$this->product();
		}
		elseif(in_array($seg, $this->data["header_link"]["news"])){
			$this->data["menu_active"] = "news";
			$this->get_link();
			$this->data["curr_link"] .= $link["news"] . "/";
			$this->news();
		}
		elseif(in_array($seg, $this->data["header_link"]["contact"])){
			$this->data["curr_link"] .= $link["contact"] . "/";
			$this->data["menu_active"] = "contact";
			$this->get_link();				
			$this->contact();
		}
		else{
			if(!empty($seg)){
				$category_list = $this->categoryModel->get_where(array("category_url" => $seg, "visible" => "y"));
				$category = $category_list->row();
				if(!empty($category)){
					if($category->lan != $this->data["language"])
						$category = $this->categoryModel->get_where(array("category.category_id" => $category->category_id, "lan" => $this->data["language"]))->row();
					$this->data["curr_link"] = base_index . $category->category_url . "/";
					$this->data["curr_menu"] = $category->categoryName;
					$this->data["category_active"] = $category->category_id;
					$this->data["menu_active"] = "";
					$this->get_link();
					$this->category($category);						
				}			
				else{
					$this->data["curr_link"] = base_index . $link["product"] . "/";
					$this->data["products"] = $this->productModel->get_front_product($this->data["language"], "`feature` = 'y' AND `visible` = 'y'");
					$this->data["featureProduct"] = render(APPPATH . "views/product_list_item.php", $this->data);
					$this->data["curr_link"] = base_index;
					$this->get_link();
					$this->load->view('index', $this->data);
				}
			}
			else{
				$this->data["curr_link"] = base_index . $link["product"] . "/";
				$this->data["products"] = $this->productModel->get_front_product($this->data["language"], "`feature` = 'y' AND `visible` = 'y'");
				$this->data["featureProduct"] = render(APPPATH . "views/product_list_item.php", $this->data);
				$this->data["curr_link"] = base_index;
				$this->get_link();
				$this->load->view('index', $this->data);				
			}
		}
	}
	
	function intro()
	{	
		$site_general = $this->settingModel->get_site_general("intro", $this->data["arr_lang"]);
		$content = $this->settingModel->get_site_generalLan(array("code"=> $site_general["code"], "lan" => $this->data["language"]));
		@$this->data["content"] = $content["content"];
		$this->load->view('intro', $this->data);
	}
	
	function product()
	{
		$seg = $this->uri->segment(2);
		if(!empty($seg)){
			if($seg == stripUnicode($this->data["lang"]["general"]["search"])){
				$this->product_search();									
			}
			else{
				$product_list = $this->productModel->get_where(array("product_url" => $seg, "visible" => "y"));
				$product = $product_list->row();
				if(!empty($product)){
					if($product->lan != $this->data["language"])
						$product = $this->productModel->get_where(array("product.product_id" => $product->product_id, "lan" => $this->data["language"]))->row();
					$this->data["product"] = $product;				
					$this->data["products"] = $this->productModel->get_all(4, 0, "RAND()", array("lan" => $this->data["language"], "visible" => "y", "product.product_id !=" => $product->product_id, "category_id" => $product->category_id));
					$this->data["other_product"] = render(APPPATH . "views/product_list_item.php", $this->data);
					$this->data["pageTitle"] = $product->productName;
					$this->load->view('product_detail', $this->data);
				}			
				else{
					$this->product_list();
				}
			}
		}
		else{
			$this->product_list();
		}				
	}
	
	function product_search()
	{
		$keyword = $_GET[$this->data["lang"]["general"]["keyword"]];
		$this->data["curr_link"] = base_index . stripUnicode($this->data["lang"]["navi"]["product"]) . "/";
		$this->data["curr_menu"] = $this->data["lang"]["general"]["search"];
		$total = $this->productModel->count_search($keyword);
		if(!empty($total)){
			$start = ($this->data["page"] - 1) * $this->data["product_paging_value"];
			$this->data["total_page"] = ceil($total / $this->data["product_paging_value"]);
			$this->data["products"] = $this->productModel->search($keyword, $this->data["language"]);
			$this->data["products"] = render(APPPATH . "views/product_list_item.php", $this->data);
		}
		else{
			$this->data["total_page"] = 0;
			$this->data["products"] = $this->data["lang"]["general"]["no_result_search"];
		}
		$this->load->view('product', $this->data);				
	}
	
	function product_list()
	{
		$total = $this->productModel->count_all(array("visible" => "y"));
		if(!empty($total)){
			$start = ($this->data["page"] - 1) * $this->data["product_paging_value"];
			$this->data["total_page"] = ceil($total / $this->data["product_paging_value"]);
			$this->data["products"] = $this->productModel->get_all($this->data["product_paging_value"], $start, "position", array("lan" => $this->data["language"], "visible" => "y"));
			$this->data["products"] = render(APPPATH . "views/product_list_item.php", $this->data);
		}
		else{
			$this->data["total_page"] = 0;
			$this->data["products"] = $this->data["lang"]["general"]["no_result"];
		}
		$this->load->view('product', $this->data);
	}
	
	function news()
	{		
		$seg = $this->uri->segment(2);
		if(!empty($seg)){
			$news_list = $this->newsModel->get_where(array("news_url" => $seg, "visible" => "y"));
			$news = $news_list->row();
			if(!empty($news)){
				if($news->lan != $this->data["language"])
					$news = $this->newsModel->get_where(array("news.news_id" => $news->news_id, "lan" => $this->data["language"]))->row();
				$this->data["news"] = $news;				
				$this->data["other_news"] = $this->newsModel->get_all(8, 0, "createdDate desc", array("lan" => $this->data["language"], "visible" => "y", "news.news_id !=" => $news->news_id));
				$this->load->view('news_detail', $this->data);
			}			
			else{
				$this->news_list();
			}
		}
		else{
			$this->news_list();
		}
	}
	
	function news_list()
	{		
		$total = $this->newsModel->count_all(array("visible" => "y"));
		if(!empty($total)){
			$start = ($this->data["page"] - 1) * $this->data["news_paging_value"];
			$this->data["total_page"] = ceil($total / $this->data["news_paging_value"]);
			$this->data["news"] = $this->newsModel->get_all($this->data["news_paging_value"], $start, "createdDate desc", array("lan" => $this->data["language"], "visible" => "y"));
		}
		else{
			$this->data["total_page"] = 0;
			$this->data["news"] = $this->data["lang"]["general"]["no_result"];
		}
		$this->load->view('news', $this->data);
	}
	
	function contact()
	{
		$this->data["msg"] = "";
		$errormsg_name = "";
		$errormsg_email = "";
		$errormsg_content = "";	
		$errormsg_captcha = "";
		
		$name = $this->input->post('name');
		$tel = $this->input->post('tel');
		$email = $this->input->post('email');
		$address = $this->input->post('address');
		$content = $this->input->post('content');
		$capcha = $this->input->post('capcha');
		
		if(!empty($_POST)){
			$err = false;			
			if(empty($name)){
				$errormsg_name .= "<div class=\"clear_left\"></div><label>&nbsp;</label><span class=\"required\">".$this->data["lang"]["contact"]["errormsg_name"]."</span>";;
				$err = true;
			}
			if(empty($email)){
				$errormsg_email .= "<div class=\"clear_left\"></div><label>&nbsp;</label><span class=\"required\">".$this->data["lang"]["contact"]["errormsg_email"]."</span>";;
				$err = true;
			}
			else{
				$regex = "/^([a-z0-9_\.-])+@(([a-z0-9_-])+\\.)+[a-z]{2,4}$/";		
				if(!preg_match($regex, $email)){
					$errormsg_email .= "<div class=\"clear_left\"></div><label>&nbsp;</label><span class=\"required\">".$this->data["lang"]["contact"]["errormsg_email2"]."</span>";
					$err = true;
				}
			}			
			if(empty($content)){
				$errormsg_content .= "<div class=\"clear_left\"></div><label>&nbsp;</label><span class=\"required\">".$this->data["lang"]["contact"]["errormsg_content"]."</span>";
				$err = true;
			}
			if(empty($capcha)){
				$errormsg_captcha = "<div class=\"clear_left\"></div><label>&nbsp;</label><span class=\"required\">".$this->data["lang"]["general"]["errormsg_captcha"]."</span>";
				$_SESSION["chk_capcha"] = false;
				$err = true;
			}
			elseif(strtolower($capcha) != strtolower(@$_SESSION["chk_captcha"])){
				$errormsg_captcha = "<div class=\"clear_left\"></div><label>&nbsp;</label><span class=\"required\">".$this->data["lang"]["general"]["errormsg_captcha2"]."</span>";
				$_SESSION["chk_capcha"] = false;
				$err = true;
			}
			else
				$_SESSION["chk_capcha"] = true;

			if(!$err){
				$this->data["msg"] = $this->data["lang"]["contact"]["msg"];
				$data = array();
				$data["name"] = $name;
				$data["lan"] = $this->data["language"];
				$data["tel"] = $tel;
				$data["email"] = $email;
				$data["address"] = $address;
				$data["content"] = $content;
				
				$this->contactModel->insert_entry($data);	
				
				unset($_SESSION["chk_capcha"]);			
			}
		}
		$this->data["name"] = $name;
		$this->data["tel"] = $tel;
		$this->data["email"] = $email;
		$this->data["address"] = $address;
		$this->data["content"] = $content;
		
		$this->data["errormsg_name"] = $errormsg_name;
		$this->data["errormsg_email"] = $errormsg_email;
		$this->data["errormsg_content"] = $errormsg_content;
		$this->data["errormsg_captcha"] = $errormsg_captcha;
		
		$this->data["text_cap"] = "";
		if(isset($_SESSION["chk_capcha"]))
			if($_SESSION["chk_capcha"] === true)
				$this->data["text_cap"] = $capcha;
		$this->load->view('contact', $this->data);
	}
	
	function category($category)
	{	
		$seg = $this->uri->segment(2);
		if(!empty($seg)){
			$product_list = $this->productModel->get_where(array("product_url" => $seg, "visible" => "y"));
			$product = $product_list->row();
			if(!empty($product)){
				if($product->lan != $this->data["language"])
					$product = $this->productModel->get_where(array("product.product_id" => $product->product_id, "lan" => $this->data["language"]))->row();
				$this->data["product"] = $product;				
				$this->data["products"] = $this->productModel->get_all(4, 0, "RAND()", array("lan" => $this->data["language"], "visible" => "y", "product.product_id !=" => $product->product_id, "category_id" => $product->category_id));
				$this->data["other_product"] = render(APPPATH . "views/product_list_item.php", $this->data);
				$this->load->view('product_detail', $this->data);
			}			
			else{
				$this->category_list($category);
			}
		}
		else{
			$this->category_list($category);
		}
				
	}
	
	function category_list($category)
	{
		
		$total = $this->productModel->count_all(array("visible" => "y", "category_id" => $category->category_id));
		if(!empty($total)){
			$start = ($this->data["page"] - 1) * $this->data["product_paging_value"];
			$this->data["total_page"] = ceil($total / $this->data["product_paging_value"]);
			$this->data["products"] = $this->productModel->get_all($this->data["product_paging_value"], $start, "position", array("lan" => $this->data["language"], "visible" => "y", "category_id" => $category->category_id));
			$this->data["products"] = render(APPPATH . "views/product_list_item.php", $this->data);
		}
		else{
			$this->data["total_page"] = 0;
			$this->data["products"] = $this->data["lang"]["general"]["no_result"];
		}
		$this->load->view('product', $this->data);
	}
}